﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common
{
    /// <summary>
    /// 验证码
    /// </summary>
    public class ValidateCodeHelper
    {
        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="validateCode"></param>
        /// <param name="codeWidth"></param>
        /// <param name="codeHeight"></param>
        /// <param name="fontSize"></param>
        /// <returns></returns>
        public byte[] CreateValidateGraphic(int vCodeLen, int codeWidth, int codeHeight, int fontSize, ImageFormat imgFormat, ref string validateCode)
        {
            string chkCode = string.Empty;
            Color[] color = new Color[]
            {
                Color.Black,
                Color.Red,
                Color.Blue,
                Color.Green,
                Color.Orange,
                Color.Brown,
                Color.Brown,
                Color.DarkBlue
            };
            string[] font = new string[]
            {
                "Times New Roman",
                "Verdana",
                "Arial",
                "Gungsuh",
                "Impact"
            };
            char[] character = new char[]
            {
                '2',
                '3',
                '4',
                '5',
                '6',
                '8',
                '9',
                'a',
                'b',
                'd',
                'e',
                'f',
                'h',
                'k',
                'm',
                'n',
                'r',
                'x',
                'A',
                'B',
                'C',
                'D',
                'E',
                'F',
                'G',
                'H',
                'J',
                'K',
                'M',
                'N',
                'P',
                'R',
                'S',
                'T',
                'W',
                'X'
            };
            Random rnd = new Random();
            for (int i = 0; i < vCodeLen; i++)
            {
                chkCode += character[rnd.Next(character.Length)];
            }
            validateCode = chkCode;
            Bitmap bmp = new Bitmap(codeWidth, codeHeight);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);

            try
            {
                for (int i = 0; i < 1; i++)
                {
                    int x = rnd.Next(codeWidth);
                    int y = rnd.Next(codeHeight);
                    int x2 = rnd.Next(codeWidth);
                    int y2 = rnd.Next(codeHeight);
                    Color clr = color[rnd.Next(color.Length)];
                    g.DrawLine(new Pen(clr), x, y, x2, y2);
                }
                for (int i = 0; i < chkCode.Length; i++)
                {
                    string fnt = font[rnd.Next(font.Length)];
                    Font ft = new Font(fnt, (float)fontSize);
                    Color clr = color[rnd.Next(color.Length)];
                    g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18f + 2f, 0f);
                }
                for (int i = 0; i < 100; i++)
                {
                    int x3 = rnd.Next(bmp.Width);
                    int y3 = rnd.Next(bmp.Height);
                    Color clr = color[rnd.Next(color.Length)];
                    bmp.SetPixel(x3, y3, clr);
                }
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, imgFormat);
                return ms.ToArray();
            }
            finally
            {
                bmp.Dispose();
                g.Dispose();
            }
        }
    }

}
